package demon1;

public class MyLinkedList <T> implements IMyLinkedList<T>{
    class ListNode{
        ListNode prev;
        ListNode next;
        public T val;
        public ListNode(T val){
            this.val=val;
        }
    }
    public ListNode head;
    public ListNode last;
    @Override
    //头插
    public void addFirst(T data) {
        ListNode node=new ListNode(data);
        if(head==null){
            head=last=node;
        }else{
            node.next=head;
            head.prev=node;
            head=node;
        }
    }

    @Override
    //尾插
    public void addLast(T data) {
        ListNode node=new ListNode(data);
        if(head==null){
            head=last=node;
        }else{
            last.next=node;
            node.prev=last;
            last=node;
        }
    }
    public void checkIndex(int index){
        if(index<0||index>size()){
            throw new IndexExp("index 位置不合法");
        }
    }
    public ListNode findIndex(int index){
        ListNode cur=head;
        while(index!=0){
            cur=cur.next;
            index--;
        }
        return cur;
    }
    @Override
    public void addIndex(int index, T data) {
        ListNode node=new ListNode(data);
        try{
            checkIndex(index);
        }catch (IndexExp e){
            e.printStackTrace();
        }
        if(index==0){
            addFirst(data);
            return;
        }
        if(index==size()){
            addLast(data);
            return;
        }
        //查找index的位置
        ListNode cur=findIndex(index);
        node.next=cur;
        cur.prev.next=node;
        node.prev=cur.prev;
        cur.prev=node;
    }

    @Override
    public boolean contains(T key) {
        if(head==null){
            return false;
        }
        ListNode cur=head;
        while(cur!=null){
            if(cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }

    @Override
    public void remove(T key) {
        if(head==null){
            return;
        }
        ListNode cur=head;
        while(cur!=null){
            if(cur.val==key) {
                if (cur == head) {
                    head = head.next;
                    if (head != null) {
                        head.prev = null;
                    } else {
                        last = null;
                    }
                } else {
                        cur.prev.next=cur.next;

                }
            }
        }

    }

    @Override
    public void removeAllKey(int key) {

    }

    @Override
    public int size() {
        ListNode cur=head;
        int count=0;
        while(cur!=null) {
            cur=cur.next;
            count++;
        }

        return count;
    }

    @Override
    public void clear() {
        ListNode cur = head;
        while (cur != null) {
            ListNode curN = cur.next;
            //cur.val = null;
            cur.prev = null;
            cur.next = null;
            cur = curN;
        }
        head = last = null;
    }
    }

    @Override
    public void display() {
        ListNode cur=head;
        while(cur!=null){
            System.out.println(cur.val);
            cur=cur.next;
        }
    }
}
